<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['setting']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$return['list']=array();
	$return['params']=array('status'=>100);
	set_html('title','订单列表');
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){

	$post=get_postdata();
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if(isset($_RQ['status']) && $_RQ['status'] != 100){
		if($_RQ['status']==1){
			$params['isover']=0;
		}
		if($_RQ['status']==2){
			$params['isover']=1;
		}
		$return['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($post['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_sendgoods_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	foreach($list as &$item){
		$goodsid=pdo_getcolumn('jy_sendgoods_order',array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'goodsid');
		$item['goods']=pdo_get('jy_weishop_goods',array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		//$item['store']=pdo_get('jy_weishop_store',array('id'=>$item['storeid'],'uniacid'=>$_SESSION['uniacid']));
		$item['createtime']=date("Y-m-d H:i",$item['createtime']);
	}
	$return['list']=$list;
	//exi(count($list),'error');exit;
	exi($return);
}
if($_RQ['op']=='pay'){
	$post=get_postdata();
	if(!$post['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_sendgoods_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['id']));
	$params['fee']=$order['fee'];
	$params['title']='支付订单';
	$params['id']=$post['id'];
	$params['plugin']='jy_sendgoods';
	$params['back_url']='jy_sendgoods/order/payresult';
	$payid=cfc('pay')->create_pay_order($params);
		
	$return['payid']=$payid;
	$return['id']=$post['id'];
	exi($return);
}
if($_RQ['op']=='delete'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_sendgoods_order',array('id'=>$_RQ['id']));
	exi('订单删除成功');
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_sendgoods_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	
	pdo_update('jy_sendgoods_order',array('ispay'=>1,'status'=>1,'paytime'=>TIMESTAMP),array('id'=>$order['id']));
	exi('支付成功','success','jy_sendgoods/order/getlist/status=1');
}	
?>